home *** CD-ROM | disk | FTP | other *** search
- Path: news.platinum.com!news
- From: Michael Scott <mscott@platinum.com>
- Newsgroups: comp.lang.c++
- Subject: Re: C++ beginner quesion on data member access.
- Date: Fri, 12 Apr 1996 08:05:36 -0700
- Organization: Platinum
- Message-ID: <316E7140.C5D@platinum.com>
- References: <4kgb76$r3s@HOPPER.ACM.ORG>
- NNTP-Posting-Host: ns2-ssn.platinum.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- Ken Varn wrote:
- >
- > I have been struggling with the proper way to declare and access data
- > members in my classes. I was once told that a class should not allow any
- > data members to be public and that they should only be accessed via member
- > functions. Why? I know the reasons for declaring private data, but what do
- > I gain if all I am doing is providing a member function to get the private
- > data or set the private data. i.e. why call a getData() function that
- > basically just returns the private data member as opposed to just declaring
- > the pviate data member as public.
-
- Classes encapsulate the behavior of an object. Data members
- represent the "state" of an object. Data access functions
- provide access to the internal state of an object while allowing
- the implementation of that state to vary without necessarily
- changing the interface to the object.
-
- Pragmatically speaking though, there are classes and
- applications where the trade-offs between encapsulation overhead
- and direct public member access favor the public data.
-
- Balancing the trade-offs is what makes programming an art rather
- than a strict science.
-